package com.yuesystem.controller;

import com.yuesystem.common.R;
import com.yuesystem.dto.requestbody.CollectRequestBody;
import com.yuesystem.dto.responsebody.CollectFolderResponseBody;
import com.yuesystem.dto.responsebody.CollectItemResponseBody;
import com.yuesystem.service.CollectFolderService;
import com.yuesystem.service.CollectItemService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 收藏类.
 */
@RestController
@CrossOrigin
@RequestMapping("/collects")
@Slf4j
public class CollectController {
  @Autowired
  private CollectFolderService collectFolderService;
  @Autowired
  private CollectItemService collectItemService;

  /**
   * 返回一个用户的所有收藏夹.
   *
   * @return 收藏夹id和名字
   */
  @GetMapping("/folders")
  public R<List<CollectFolderResponseBody>> getFolders() {
    return R.success(collectFolderService.getUserFolder());
  }

  /**
   * 删除某个用户的收藏夹.
   *
   * @param folderId 收藏夹id
   * @return 删除是否成功
   */
  @DeleteMapping("/deleteFolder")
  public R<Boolean> deleteFolder(@RequestParam Long folderId) {
    return R.success(collectFolderService.removeFolder(folderId));
  }

  /**
   * 新增收藏夹.
   *
   * @param folderName 收藏夹名称
   * @param desc       收藏夹描述
   * @return 新增是否成功
   */
  @PutMapping("/addFolder")
  public R<String> addFolder(@RequestParam String folderName, @RequestParam String desc) {
    if (!collectFolderService.isRepeat(folderName)) {
      Boolean flag = collectFolderService.add(folderName, desc);
      if (flag) {
        return R.success("新建收藏夹成功！");
      } else {
        return R.error("新建收藏夹失败！");
      }
    } else {
      return R.error("收藏夹名重复");
    }
  }

  /**
   * flag为true表示执行取消收藏，flag为false执行收藏.
   *
   * @param body 收藏夹内的条目类
   * @return 操作是否成功
   */
  @PutMapping("/change")
  public R<String> change(@RequestBody CollectRequestBody body) {
    Boolean flag = body.getFlag();
    Long collectId = body.getCollectId();
    String type = body.getType();
    Long folderId = body.getFolderId();
    // 已经收藏过，取消收藏
    if (flag) {
      Long id = collectItemService.judge(collectId, type);
      Boolean cancel = collectItemService.cancel(id);
      if (cancel) {
        return R.success("取消成功");
      }
      return R.success("取消失败");
    } else {
      Boolean add = collectItemService.add(folderId, type, collectId);
      if (add) {
        return R.success("收藏成功");
      }
      return R.success("收藏失败");
    }
  }

  /**
   * 前台取消收藏.
   *
   * @param collectId 收藏条目id
   * @param type      收藏条目类型（官职/书院/帖子）
   * @return 操作是否成功
   */
  @DeleteMapping("/cancelCollect")
  public R<String> cancelCollect(@RequestParam Long collectId, @RequestParam String type) {
    Long id = collectItemService.judge(collectId, type);
    Boolean cancel = collectItemService.cancel(id);
    if (cancel) {
      return R.success("取消成功");
    }
    return R.error("取消失败");
  }

  /**
   * 获取某个用户的某个收藏夹下的所有条目.
   *
   * @param folderId 收藏夹id
   * @return 某个收藏夹下的条目列表
   */
  @GetMapping("/allList")
  public R<List<CollectItemResponseBody>> getCollectAllList(@RequestParam Long folderId) {
    List<CollectItemResponseBody> list = collectItemService.getAllItem(folderId);
    return R.success(list);
  }

  /**
   * 后台取消收藏.
   *
   * @param itemId 收藏条目id
   * @return 操作是否成功
   */
  @DeleteMapping("/backCancel")
  public R<String> cancelBlog2(@RequestParam Long itemId) {
    if (collectItemService.cancel(itemId)) {
      return R.success("取消收藏成功！");
    }
    return R.error("取消收藏失败！");
  }

  /**
   * 判断当前用户是否收藏过该书院.
   *
   * @param academyId 书院id
   * @return 收藏过返回true, 否则返回false
   */
  @GetMapping("/academy/judge/{academyId}")
  public R<Boolean> academyJudgeCollect(@PathVariable Long academyId) {
    Long id = collectItemService.judge(academyId, "academy");
    if (id != -1L) {
      return R.success(true);
    }
    return R.success(false);
  }

  /**
   * 判断当前用户是否收藏过该官职.
   *
   * @param officeId 官职id
   * @return 收藏过返回true, 否则返回false
   */
  @GetMapping("/office/judge/{officeId}")
  public R<Boolean> officeJudgeCollect(@PathVariable Long officeId) {
    Long id = collectItemService.judge(officeId, "office");
    if (id != -1L) {
      return R.success(true);
    }
    return R.success(false);
  }

  /**
   * 判断当前用户是否收藏过该帖子.
   *
   * @param blogId 帖子id
   * @return 收藏过返回true, 否则返回false
   */
  @GetMapping("/blog/judge/{blogId}")
  public R<Boolean> blogJudgeCollect(@PathVariable Long blogId) {
    Long id = collectItemService.judge(blogId, "blog");
    if (id != -1L) {
      return R.success(true);
    }
    return R.success(false);
  }

}
